package com.jeeplus.utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import io.protostuff.ProtobufIOUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.objenesis.Objenesis;
import org.springframework.objenesis.ObjenesisStd;

import io.protostuff.LinkedBuffer;
import io.protostuff.ProtostuffIOUtil;
import io.protostuff.Schema;
import io.protostuff.runtime.RuntimeSchema;

/**
 * ProtoBufUtil 转换工具类
 *
 * @author XRQ
 *
 */
public class ProtoBufUtil {
    private static Logger log = LoggerFactory.getLogger(ProtoBufUtil.class);
    private static Map<Class<?>, Schema<?>> cachedSchema = new ConcurrentHashMap<Class<?>, Schema<?>>();

    private static Objenesis objenesis = new ObjenesisStd(true);

    @SuppressWarnings("unchecked")
    private static <T> Schema<T> getSchema(Class<T> cls) {
        Schema<T> schema = (Schema<T>) cachedSchema.get(cls);
        if (schema == null) {
            schema = RuntimeSchema.createFrom(cls);
            if (schema != null) {
                cachedSchema.put(cls, schema);
            }
        }
        return schema;
    }

    public ProtoBufUtil() {
    }

    @SuppressWarnings({ "unchecked" })
    public static <T> byte[] serializer(T obj) {
        Class<T> cls = (Class<T>) obj.getClass();
        LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE);
        try {
            Schema<T> schema = getSchema(cls);
            return ProtobufIOUtil.toByteArray(obj, schema, buffer);
        } catch (Exception e) {
            log.error("protobuf序列化失败");
            throw new IllegalStateException(e.getMessage(), e);
        } finally {
            buffer.clear();
        }
    }

    public static <T> T deserializer(byte[] bytes, Class<T> clazz) {
//        try {
        T message = (T) objenesis.newInstance(clazz);
        Schema<T> schema = getSchema(clazz);

        //byte[] byt = new byte[1024];
        // InputStream input = new IntArrayInputStream();// new ByteArrayInputStream(byt);
//            ProtostuffIOUtil.mergeFrom(bytes, message, schema);
        ProtobufIOUtil.mergeFrom(bytes, message, schema);


        return message;
//        } catch (Exception e) {
//            log.error("protobuf反序列化失败 e="+e);
//            throw new IllegalStateException(e.getMessage(), e);
//        }
    }

//    public static int[] toByteArray(InputStream input) throws IOException {
//        ByteArrayOutputStream output = new ByteArrayOutputStream();
//        int[] buffer = new int[4096];
//        int n = 0;
//        while (-1 != (n = input.read(buffer))) {
//            output.write(buffer, 0, n);
//        }
//        return output.toByteArray();
//    }

//    public static <T> T deserializer(byte[] bytes, Class<T> clazz) {
//        try {
//
//            //InputStream
//            T message = (T) objenesis.newInstance(clazz);
//            Schema<T> schema = getSchema(clazz);
//            ProtostuffIOUtil.mergeFrom(bytes, message, schema);
//            return message;
//        } catch (Exception e) {
//            log.error("protobuf反序列化失败 e="+e);
//            throw new IllegalStateException(e.getMessage(), e);
//        }
//    }

}